<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<script type="text/javascript">
var xhr;
function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		xhr = new XMLHttpRequest();
	}
}
function adSel(){
	//alert("들어옴");
	var spn = document.getElementById("addAddress");
	var sel = document.getElementById("addressList");
	
	var subBtn = document.createElement("Input");
	subBtn.setAttribute("name","subBtn");
	subBtn.setAttribute("type","button");
	subBtn.setAttribute("id","subBtn");
	subBtn.setAttribute("value","확인");
	subBtn.setAttribute("onClick","adInput();");
	spn.appendChild(subBtn);
}
function getAddress(){
	var dong = document.getElementById("dong");
	createXMLHttpRequest();
	//선택된 대분류의 value값을 조회
	
	//var fc = asel.options[asel.selectedIndex].value;
	var url = "/hansot/getAddress.do?command=getAddress&dong="+dong.value;
	//처리를 요청
	xhr.onreadystatechange = getAddressList;
	xhr.open("GET", url, true);
	xhr.send(null);
}
function adInput(){
	var sel = document.getElementById("addressList");
	var txt = sel.options[sel.selectedIndex].text;
	
	opener.document.join.zipcode.value = txt.substring(0,8);
	opener.document.join.address1.value = txt.substring(8);
	opener.document.join.seq.value = sel.options[sel.selectedIndex].value;
	var flag = window.confirm("창을 닫으시겠습니까?");
	if(flag){
		window.close();
	}
}

function getAddressList(){
	if(xhr.readyState==4){
		if(xhr.status==200){
			var txt = xhr.responseText;
			var jsonData = eval("("+txt+")");
			
			var spn = document.getElementById("addAddress");
			
			while(spn.hasChildNodes()){
				spn.removeChild(spn.firstChild);
			}
			
			
			var sel = document.createElement("select");
			sel.setAttribute("name","addressList");
			sel.setAttribute("id","addressList");
			sel.setAttribute("onChange","adSel();");
			//select의 option 모두 삭제 후 추가해야 한다. (개수가 분류마다 다르므로)
			//삭제
			while(sel.hasChildNodes()){
				sel.removeChild(sel.firstChild);
			}
			//추가
			var adList = jsonData.list;
			//alert(jsonData.list.length);
			sel[0] = new Option("주소선택", "default");
			for(var i = 0; i<adList.length;i++){
				sel[i+1] = new Option(adList[i].zipcode+" "+adList[i].address, adList[i].seq);
				//alert(sel[i+1]);			
			}
			
			spn.appendChild(sel);
			
			//기존에 조회된 소분류와 테이블이 있으면 삭제 되어야 한다.
			//removeThirdCategory();
			//removeTable();
		}else{
			alert("중분류 요청처리 실패 : "+xhr.status+" - "+xhr.statusText);
		}
	}
}
</script>


주소찾기<br>
동을 입력하세요..ex)삼평동, 송파동<br>

<input type="text" name="dong" id="dong"><input type="button" value="주소찾기" onclick="getAddress();"><p>
<span id="addAddress"></span>
